﻿using System;

namespace ProblemsSet
{
    public class Problem_137 : BaseProblem
    {
        public override object GetResult()
        {
            const int index = 15;

            long cnt = 0;
            long n = 0;

            for (long a = 2; a <= long.MaxValue; a++)
            {
                var bb = (Math.Sqrt(5*a*a - 4)-a)/2;
                if (bb > 0 && Math.Truncate(bb) == bb)
                {
                    n = (long) (a*bb);
                    cnt++;
                    if (cnt == index)
                        return n;
                }
                bb = (Math.Sqrt(5 * a * a + 2) - 2*a) / 2;
                if (bb <= 0 || Math.Truncate(bb) != bb) continue;
                n = (long)(2 * a * bb - 1);
                cnt++;
                if (cnt == index)
                    return n;
            }
            return -1;
        }

        public override string Problem
        {
            get
            {
                return @"Consider the infinite polynomial series AF(x) = xF1 + x2F2 + x3F3 + ..., where Fk is the kth term in the Fibonacci sequence: 1, 1, 2, 3, 5, 8, ... ; that is, Fk = Fk1 + Fk2, F1 = 1 and F2 = 1.

For this problem we shall be interested in values of x for which AF(x) is a positive integer.

Surprisingly AF(1/2)	 = 	(1/2).1 + (1/2)2.1 + (1/2)3.2 + (1/2)4.3 + (1/2)5.5 + ...
 	 = 	1/2 + 1/4 + 2/8 + 3/16 + 5/32 + ...
 	 = 	2
The corresponding values of x for the first five natural numbers are shown below.

x	AF(x)
21	1
1/2	2
(132)/3	3
(895)/8	4
(343)/5	5
We shall call AF(x) a golden nugget if x is rational, because they become increasingly rarer; for example, the 10th golden nugget is 74049690.

Find the 15th golden nugget.";
            }
        }

        public override bool IsSolved
        {
            get
            {
                return true;
            }
        }

        public override object Answer
        {
            get
            {
                return 1120149658760;
            }
        }

    }
}
